package com.work.device.controller;

import com.work.device.pojo.Index;
import com.work.device.service.IndexService;
import com.work.entity.Result;
import com.work.entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author YuanGan
 * @Description : 数据指标的查询和添加
 * @Date 2020/10/16
 **/
@RestController
@RequestMapping("/index")
public class IndexController {
    @Autowired
    IndexService indexService;

    /**
     * 查询所有测量数据
     *
     * @return 查询结果
     */
    @GetMapping("/findAll")
    public Result<List<Index>> findAll() {
        try {
            List<Index> indixList = indexService.findAll();
            return new Result<List<Index>>(true, StatusCode.FIND_Ok, "测量查询成功", indixList
            );
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result<List<Index>>(false, StatusCode.FIND_ERROR, "测量查询失败");
    }

    /**
     * 根据id查询所有测量数据
     *
     * @return 查询结果
     */
    @GetMapping("/findById/{id}")
    public Result<Index> findById(@PathVariable Long id) {
        try {
            Index indix = indexService.findById(id);
            return new Result<Index>(true, StatusCode.FIND_Ok, "测量查询成功", indix);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result<Index>(false, StatusCode.FIND_ERROR, "测量查询失败");
    }

    /**
     * 添加数据指标
     *
     * @param index 数据指标
     * @return 添加结果
     */
    @PostMapping("/add")
    public Result add(@RequestBody Index index) {
        try {
            indexService.add(index);
            return new Result<Index>(false, StatusCode.ADD_OK, "新增测量指标成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result<Index>(false, StatusCode.ADD_ERROR, "新增测量指标失败");
    }
}
